<?php
namespace Admin\Controller;
use Think\Controller;
Vendor('Filehuancun.Filehuancun');//Vendor 引入Filehuancun目录下的Filehuancun命名空间
Vendor('ExcleInOut.ExcleInOut');//Vendor 引入ExcleInOut目录下的ExcleInOut命名空间


class RkclController extends Controller {
	public function deldir($path){
		// 如果是目录则继续
		if (is_dir ( $path )) {
			// 扫描一个文件夹内的所有文件夹和文件并返回数组
			$p = scandir ( $path );
			foreach ( $p as $val ) {
				// 排除目录中的.和..
				if ($val != "." && $val != "..") {
					// 如果是目录则递归子目录，继续操作
					if (is_dir ( $path . $val )) {
						// 子目录中操作删除文件夹和文件
						$this->deldir ( $path . $val . '/' );
						// 目录清空后删除空文件夹
						@rmdir ( $path . $val . '/' );
					} else {
						// 如果是文件直接删除
						unlink ( $path . $val );
					}
				}
			}
		}
	}
	public function ceshi()//ajax测试方法
	{
		//$name1 ='ThinkPHP11111111111';
		//session('wwf',$name1);
		$sess_auth = session('auth');
		echo $sess_auth['per'];
	}
	public function index() {
	 	//$this->assign('wwfarr1',$temp1);
	 	//echo $this->assign('arr1');
	    $sess_auth = session('auth');
		if($sess_auth)
		{
	    $this->assign('user',$sess_auth['user']);
		$this->display();//echo 输出和assign要在这个这个函数之前设置完成。
		}
	} 
	public function ajaxdpbm1()
	{
	
		$user1 = M('lfxmdpbm');
	
	
		$sjkTemp1=$user1->select();//后期这里要按照条件筛选
	
	
		//二维数组转换字符串
		$temp1="";
	
		for($i=0;$i<count($sjkTemp1);$i++)
		{
		$temp1=$temp1.$sjkTemp1[$i][dpmc].",";
		}
			
	
		//去除最后一个字符
		$temp1=substr ( $temp1, 0, - 1 );
	
		//打包字符串 用ajax返回。
		echo $temp1;
	}
	public function ajaxmddb()
	{
		      
		$temp1 = $_POST["arr1"];//开始日期
		$temp2 = $_POST["arr2"];//结束日期
		$temp3 = $_POST["arr3"];//单品
		$temp4 = $_POST["arr4"];//货物类型
		
		$user = M('lfxmrkb');
			
		switch($temp3)
		{
			case "全部" :
				{
					break;
				}		
				
			default :
				{
					$map['dpmc'] =$temp3;
					break;
				}
		
		}
			
		switch($temp4)
		{
			case "全部" :
				{
					break;
				}
		
			default :
				{
					$map['hwlx'] =$temp4;
					break;
				}
					
		}
		
		$map['rkrq'] = array(array('egt', $temp1),array('elt', $temp2),'and');//大于等于and小于等于    单字段 多条件  查询
		$map['_logic'] = 'and';//筛选条件
		//print_r($map); 	
		$sjkTemp=$user->where($map)->select();//后期这里要按照条件筛选
		for ($i=0;$i<count($sjkTemp);$i=$i+1){
			if($sjkTemp[$i]["jzfhbj"]=="正常"){
				$sjkTemp[$i]["jzfhbj"]="正常";
				$sjkTemp[$i]["jzfhbj1"]="禁止发货";
			}
			if($sjkTemp[$i]["jzfhbj"]!="正常"){
				$sjkTemp[$i]["jzfhbj"]="禁止发货";
				$sjkTemp[$i]["jzfhbj1"]="正常";
			}
			
		}
		//print_r($sjkTemp);
		//echo $temp1.$temp2.$temp3.$temp4;
		echo  json_encode ( $sjkTemp ,JSON_UNESCAPED_UNICODE );
	}
	 public function ajaxdpbm()
	 {
	 	$user = M('lfxmdpbm');
	 
	 	$sjkTemp=$user->select();//后期这里要按照条件筛选
	 
	 	$arrTemp2 = ""; //输出可编辑信息
	 
	 	for($ii = 0; $ii < count ( $sjkTemp ); $ii ++) {
	 		$arrTemp2 =$arrTemp2.
	 		$sjkTemp [$ii] [id].",".
	 		$sjkTemp [$ii] [hpbm].",".
	 		$sjkTemp [$ii] [dpmc].",".
	 		$sjkTemp [$ii] [sptm].",".
	 		$sjkTemp [$ii] [dpgg].",".
	 		$sjkTemp [$ii] [djzlkg].",".
	 		$sjkTemp [$ii] [dpbzq].",".
	 		$sjkTemp [$ii] [dpdw].",".
	 		$sjkTemp [$ii] [hwlx].",".
	 		$sjkTemp [$ii] [dpbzq].",";
	 
	 	}
	 	$arrTemp2=substr ( $arrTemp2, 0, - 1 );
	 	echo $arrTemp2;
	 }
	 public function ajaxtjsj()
	 {
	 	
	 	$temp1 = explode(',',$_POST["arr1"]);//td
	 	$temp2 = explode(',',$_POST["arr2"]);//th
	 	$temp3 = explode(',',$_POST["arr3"]);//input
	 	$temp4 = explode(',',$_POST["arr4"]);//select
	 	$userbf=$_POST["user"];//操作员
	 	
	 	//print_r($temp1); 
	 	//print_r($temp2);
	 	//print_r($temp3);
	 	//print_r($userbf);
	 	$user = M('lfxmrkb');
	    //input和td按3循环 th 使用$thsj
	 	$thjs=0;
	 	$tdjs=0;
	 	$seljs=0;
	 	for ($i=0;$i<count($temp3);$i=$i+3){
	 		//等于a99999新增，否则更新。
	 		if($temp3[0]=="a99999"){
	 			
	 			$data1['hpbm'] = $temp2[$thjs+2];	 			
	 			$data1['sptm'] = $temp2[$thjs+3];
	 			$data1['dpgg'] = $temp2[$thjs+4];
	 			$data1['djzlkg'] = $temp2[$thjs+5];
	 			$data1['dpbzq'] =$temp2[$thjs+7];
	 			$data1['hwlx'] = $temp2[$thjs+6];
	 			
	 			$data1['dpmc'] = $temp1[$tdjs];
	 			$data1['rksl'] = $temp1[$tdjs+2];
	 			$data1['dpbz'] = $temp1[$tdjs+1];
	 			$data1['dpkw'] = $temp1[$tdjs+3];
	 			
	 			$data1['rkdppc'] = $temp3[$i+1];
	 			$data1['rkrq'] = $temp3[$i+2];
	 			
	 			$data1['jzfhbj'] = $temp4[$seljs];
	 			
	 			$data1['ddscz'] = $userbf;
	 			$today  =  date ( "Y-m-d H:i:s" );
	 			$data1 ['scrqsj'] = $today;
	 			
	 			$user->add($data1);
	 			$tdjs=$tdjs+4;
	 			$thjs=$thjs+13;
	 			$seljs=$seljs+1;
	 		}else{
	 			$data1['hpbm'] = $temp2[$thjs+2];
	 			$data1['hpbm'] = $temp2[$thjs+2];
	 			$data1['sptm'] = $temp2[$thjs+3];
	 			$data1['dpgg'] = $temp2[$thjs+4];
	 			$data1['djzlkg'] = $temp2[$thjs+5];
	 			$data1['dpbzq'] =$temp2[$thjs+7];
	 			$data1['hwlx'] = $temp2[$thjs+6];
	 				
	 			$data1['dpmc'] = $temp1[$tdjs];
	 			$data1['rksl'] = $temp1[$tdjs+2];
	 			$data1['dpbz'] = $temp1[$tdjs+1];
	 			$data1['dpkw'] = $temp1[$tdjs+3];

	 			$data1['id'] = $temp3[$i];
	 			$data1['rkdppc'] = $temp3[$i+1];
	 			$data1['rkrq'] = $temp3[$i+2];
	 			
	 			$data1['jzfhbj'] = $temp4[$seljs];
	 			
	 			$data1['ddscz'] = $userbf;
	 			$today  =  date ( "Y-m-d H:i:s" );
	 			$data1 ['scrqsj'] = $today;
	 				
	 			$user->save($data1);
	 			$tdjs=$tdjs+4;
	 			$thjs=$thjs+13;
	 			$seljs=$seljs+1;
	 		}
	 	}	 	
	 	
	 } 
	 public function tjajaxsjsc()
	 {
	 	//新增司机和原有司机在一个表格中编辑，新增的复选框值是a99999，删除是直接去掉a99999，只在数据库中删除正常有id的。
	 	$temp2 = $_POST["arr2"];
	 	$temp2=str_replace('a99999,','',$temp2);
	 	$temp2 = substr($temp2,0,strlen($temp2)-1);
	 	$user = M('lfxmrkb');
	 	$user->delete($temp2);//使用1,2,3这样的id字符串，删除数据库。
	 	//var_dump($temp2);
	 }
	 public function ajaxdppccx()
	 {
	 
	 	$today  =  date ( "Y-m-d" );//当天 计算当天（包含当天）的前的全部入库和出库。
	 
	 	$userrk = M('lfxmrkb');
	 	$userck = M('lfxmckb');
	 	$userdp = M('lfxmdpbm');
	 
	 	$mapjzrqs['rkrq'] =array('elt', $today);//入库日期
	 	$mapjzrqe['ckrq'] =array('elt', $today);//出库日期
	 		
	 
	 	$sjkTemp1=$userrk->where($mapjzrqs)->select();
	 	$sjkTemp2=$userck->where($mapjzrqe)->select();
	 	$sjkTemp3=$userdp->select();
	 	//去除订单编码的重复项，并排序。
	 
	 	//var_dump($sjkTemp1);
	 	//库存标记数组。单品+批次。
	 	$arrtemp1=array();//入库
	 	$arrtempjs=0;
	 	for($ii = 0; $ii < count ( $sjkTemp1 ); $ii ++) {
	 		 
	 		$arrtemp1[$arrtempjs]=$sjkTemp1[$ii][dpmc]."+".$sjkTemp1[$ii][rkdppc];
	 		$arrtempjs=$arrtempjs+1;
	 	}
	 	//var_dump($arrtemp1);
	 	 
	 	$arrtemp2=array();//出库
	 	$arrtempjs=0;
	 	for($ii = 0; $ii < count ( $sjkTemp2 ); $ii ++) {
	 
	 		$arrtemp2[$arrtempjs]=$sjkTemp2[$ii][pinming]."+".$sjkTemp2[$ii][ckpc];
	 		$arrtempjs=$arrtempjs+1;
	 	}
	 	//var_dump($arrtemp2);
	 
	 	//合并数组
	 	$newarr = array_merge($arrtemp1, $arrtemp2);
	 	//var_dump($newarr);
	 	//去除重复项
	 	$arrddbmaa=  array_unique ( $newarr );
	 	sort ( $arrddbmaa );
	 	//var_dump($arrddbmaa);
	 	//重新按 + 号分隔数组。
	 	$dppcarr=array();
	 	for($ia = 0; $ia < count ( $arrddbmaa ); $ia ++) {
	 		$temp=explode('+',$arrddbmaa[$ia]);
	 		//var_dump($temp);
	 		$dppcarr[$ia]["pinming"]=$temp[0];
	 		$dppcarr[$ia]["pici"]=$temp[1];
	 	}
	 	//var_dump($dppcarr);
	 
	 	//生成最终的入库出库数据 生成库存和虚拟库存。
	 	$zzsj=array();
	 
	 	//赋予单品属性
	 	for($aa = 0; $aa < count ( $dppcarr ); $aa ++) {
	 		for($bb = 0; $bb < count ( $sjkTemp3 ); $bb ++) {
	 
	 			if($dppcarr[$aa]["pinming"]==$sjkTemp3[$bb]["dpmc"]){
	 				$zzsj[$aa]["pinming"]= $dppcarr[$aa]["pinming"];
	 				$zzsj[$aa]["pici"]=$dppcarr[$aa]["pici"];
	 				$zzsj[$aa]["hpbm"]=$sjkTemp3[$bb]["hpbm"];
	 				$zzsj[$aa]["sptm"]=$sjkTemp3[$bb]["sptm"];
	 				$zzsj[$aa]["dpgg"]=$sjkTemp3[$bb]["dpgg"];
	 				$zzsj[$aa]["djzlkg"]=$sjkTemp3[$bb]["djzlkg"];
	 				$zzsj[$aa]["dpbzq"]=$sjkTemp3[$bb]["dpbzq"];
	 				$zzsj[$aa]["hwlx"]=$sjkTemp3[$bb]["hwlx"];
	 			}
	 	   
	 		}
	 	}
	 	//加一个变量 保存 赋予属性后的品名+批次数组。
	 	//$arrpmjpcsj=$zzsj;
	 	//session ('arrpmjpcsj' , $arrpmjpcsj);
	 	//计算单品+品名的 入库件数。
	 	for($aa = 0; $aa < count ( $zzsj ); $aa ++) {
	 		$tempjsjs=0;
	 		for($bb = 0; $bb < count ( $sjkTemp1 ); $bb ++) {
	 
	 			//提取单品属性，找到就退出当前循环。
	 			if($zzsj[$aa]["pinming"]==$sjkTemp1[$bb]["dpmc"]&&$zzsj[$aa]["pici"]==$sjkTemp1[$bb]["rkdppc"]){
	 				$tempjsjs=$tempjsjs+$sjkTemp1[$bb]["rksl"];
	 			}
	 		}
	 		$zzsj[$aa]["rksl"]=$tempjsjs;
	 	}
	 
	 	//计算单品+品名的 出库件数分为虚拟和真实。
	 	for($aa = 0; $aa < count ( $zzsj ); $aa ++) {
	 
	 		$tempjsjs1=0;
	 		$tempjsjs2=0;
	 		for($bb = 0; $bb < count ( $sjkTemp2 ); $bb ++) {
	 
	 			//虚拟库存 计算  只要订单不作废 就计算
	 			if($zzsj[$aa]["pinming"]==$sjkTemp2[$bb]["pinming"]&&$zzsj[$aa]["pici"]==$sjkTemp2[$bb]["ckpc"]&&$sjkTemp2[$bb]["ddzfbj"]!="已作废"){
	 				$tempjsjs1=$tempjsjs1+$sjkTemp2[$bb]["shjs"];
	 			}
	 
	 
	 			//真实库存 计算  只要订单不作废 已过账  就计算
	 			if($zzsj[$aa]["pinming"]==$sjkTemp2[$bb]["pinming"]&&$zzsj[$aa]["pici"]==$sjkTemp2[$bb]["ckpc"]&&$sjkTemp2[$bb]["ddzfbj"]!="已作废"&&$sjkTemp2[$bb]["gzbj"]=="已过账"){
	 				$tempjsjs2=$tempjsjs2+$sjkTemp2[$bb]["shjs"];
	 			}
	 		}
	 
	 		$zzsj[$aa]["xnkc"]=(string)($zzsj[$aa]["rksl"]-$tempjsjs1);
	 		$zzsj[$aa]["zskc"]=(string)($zzsj[$aa]["rksl"]-$tempjsjs2);
	 	  
	 		$zzsj[$aa]["xnsm"]=(string)$tempjsjs1;
	 		$zzsj[$aa]["gzsm"]=(string)$tempjsjs2;
	 	}
	 	//var_dump($zzsj);
	 	 
	 	echo  json_encode ( $zzsj ,JSON_UNESCAPED_UNICODE );
	 	//echo  json_encode ( $sjkTemp1 ,JSON_UNESCAPED_UNICODE ).fengge.json_encode ( $sjkTemp2 ,JSON_UNESCAPED_UNICODE );
	 }
}

